package 基础数学;

/**
 * @author 帅小伙
 * @date 2021/12/28
 * @description  孩子们的游戏(圆圈中最后剩下的数)
 */
public class Demo02JZ62 {


    public int LastRemaining_Solution(int n, int m) {
        if (n < 1 || m < 1) return -1;
        int[] array = new int[n];
        int i = -1, step = 0, count = n;
        while (count > 0) {
            i++;
            if (i >= n) i = 0;
            if (array[i] == -1) continue;
            step++;
            if (step == m) {
                array[i] = -1;
                step = 0;
                count--;
            }
        }
        return i;
    }
}
